package columnd

import (
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

const (
	// DomainID 领域唯一标志
	DomainID = "column"
)

// DomainOption 域构建参数
type DomainOption struct {
	ColumnRepo    ColumnRepo
	ExtraTypeRepo ExtraTypeRepo
}

// NewDomain 核心子域内容信息域
func NewDomain(p ddd.Domain, opt DomainOption) ColumnDomain {
	d := &columnDomain{}
	d.Init(d, p, DomainID)
	initColumn(d, opt.ColumnRepo)
	initExtra(d, opt.ExtraTypeRepo)
	return d
}

// ColumnDomain 信息域
type ColumnDomain interface {
	ddd.Domain
	ColumnAggregateRoot() ColumnAggregateRoot
	ExtraTypeAggregteRoot() ExtraTypeAggregteRoot
}
type columnDomain struct {
	ddd.BaseDomain
	columnAggregateRoot   ColumnAggregateRoot
	extraTypeAggregteRoot ExtraTypeAggregteRoot
}

func (i *columnDomain) ColumnAggregateRoot() ColumnAggregateRoot {
	if i.columnAggregateRoot != nil {
		return i.columnAggregateRoot
	}
	r := i.BaseDomain.AggregateRootByID("column")
	if r == nil {
		panic("没有初始化ColumnAggregateRoot")
	}
	ir, ok := r.(ColumnAggregateRoot)
	if !ok {
		panic("初始化ColumnAggregateRoot，类型错误")
	}
	i.columnAggregateRoot = ir
	return ir
}
func (i *columnDomain) ExtraTypeAggregteRoot() ExtraTypeAggregteRoot {
	if i.extraTypeAggregteRoot != nil {
		return i.extraTypeAggregteRoot
	}
	r := i.BaseDomain.AggregateRootByID(EvtTypExtra)
	if r == nil {
		panic("没有初始化ExtraTypeAggregteRoot")
	}
	ir, ok := r.(ExtraTypeAggregteRoot)
	if !ok {
		panic("初始化ExtraTypeAggregteRoot，类型错误")
	}
	i.extraTypeAggregteRoot = ir
	return ir
}
